//
// Decompiled by Jadx - 435ms
//
package fk;

import android.content.Context;
import android.database.SQLException;
import android.text.TextUtils;
import android.util.Log;
import dd.a;
import fl.a;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import me.plusne.GreenDao.dao.CacheApiDBDao;
import me.plusne.GreenDao.dao.ChatLogDao;
import me.plusne.GreenDao.dao.ConfigDao;
import me.plusne.GreenDao.dao.ConfigSystemDao;
import me.plusne.GreenDao.dao.DownLogDao;
import me.plusne.GreenDao.dao.GetValueVersionDao;
import me.plusne.GreenDao.dao.LookHomeJsonDao;
import me.plusne.GreenDao.dao.ParserDataDao;
import me.plusne.GreenDao.dao.RecordLogDao;
import me.plusne.GreenDao.dao.SoulAutoLogDao;
import me.plusne.GreenDao.dao.SoulChatListHideDao;
import me.plusne.GreenDao.dao.SoulVoiceCollectDao;
import me.plusne.GreenDao.dao.TopLabelDbDao;
import me.plusne.GreenDao.dao.WeChatMessageDao;

public final class b extends a.a {
    public b(Context context) {
        super(context);
    }

    public final void a(org.greenrobot.greendao.database.Database aVar) {
        String str;
        Class[] clsArr;
        String str2;
        int i = 14;
        int i2 = 0;
        boolean z = true;
        Class[] clsArr2 = {ConfigDao.class, ConfigSystemDao.class, RecordLogDao.class, TopLabelDbDao.class, DownLogDao.class, ParserDataDao.class, SoulVoiceCollectDao.class, GetValueVersionDao.class, LookHomeJsonDao.class, ChatLogDao.class, SoulChatListHideDao.class, SoulAutoLogDao.class, WeChatMessageDao.class, CacheApiDBDao.class};
        dd.a.a = new WeakReference(new a());
        int i3 = 0;
        while (true) {
            str = null;
            if (i3 >= 14) {
                break;
            }
            g.a aVar2 = new g.a(aVar, clsArr2[i3]);
            String str3 = aVar2.b;
            if (dd.a.b(aVar, false, str3)) {
                try {
                    str = str3.concat("_TEMP");
                    aVar.d("DROP TABLE IF EXISTS " + str + ";");
                    aVar.d("CREATE TEMPORARY TABLE " + str + " AS SELECT * FROM " + str3 + ";");
                    StringBuilder sb = new StringBuilder();
                    int i4 = 0;
                    while (true) {
                        String[] strArr = aVar2.d;
                        if (i4 >= strArr.length) {
                            break;
                        }
                        sb.append(strArr[i4]);
                        sb.append(",");
                        i4++;
                    }
                    if (sb.length() > 0) {
                        sb.deleteCharAt(sb.length() - 1);
                    }
                } catch (SQLException e) {
                    Log.e("MigrationHelper", "【Failed to generate temp table】" + str, e);
                }
            }
            i3++;
        }
        WeakReference weakReference = dd.a.a;
        if (weakReference != null) {
            str = (a.a) weakReference.get();
        }
        if (str != null) {
            str.b(aVar);
            str.a(aVar);
        } else {
            dd.a.c(aVar, "dropTable", true, clsArr2);
            dd.a.c(aVar, "createTable", false, clsArr2);
        }
        while (i2 < i) {
            String str4 = new g.a(aVar, clsArr2[i2]).b;
            String concat = str4.concat("_TEMP");
            if (!dd.a.b(aVar, z, concat)) {
                clsArr = clsArr2;
            } else {
                try {
                    ArrayList<a.b> g = a.b.g(aVar, str4);
                    ArrayList<a.b> g2 = a.b.g(aVar, concat);
                    ArrayList arrayList = new ArrayList(g.size());
                    ArrayList arrayList2 = new ArrayList(g.size());
                    for (a.b bVar : g2) {
                        if (g.contains(bVar)) {
                            StringBuilder sb2 = new StringBuilder();
                            clsArr = clsArr2;
                            try {
                                sb2.append('`');
                                sb2.append(bVar.b);
                                sb2.append('`');
                                String sb3 = sb2.toString();
                                arrayList2.add(sb3);
                                arrayList.add(sb3);
                            } catch (SQLException e2) {
                                e = e2;
                                Log.e("MigrationHelper", "【Failed to restore data from temp table 】" + concat, e);
                                i2++;
                                clsArr2 = clsArr;
                                i = 14;
                                z = true;
                            }
                        } else {
                            clsArr = clsArr2;
                        }
                        clsArr2 = clsArr;
                    }
                    clsArr = clsArr2;
                    for (a.b bVar2 : g) {
                        if (bVar2.d && !g2.contains(bVar2)) {
                            String str5 = '`' + bVar2.b + '`';
                            arrayList2.add(str5);
                            if (bVar2.e != null) {
                                str2 = "'" + bVar2.e + "' AS ";
                            } else {
                                str2 = "'' AS ";
                            }
                            arrayList.add(str2 + str5);
                        }
                    }
                    if (arrayList2.size() != 0) {
                        aVar.d("REPLACE INTO " + str4 + " (" + TextUtils.join(",", arrayList2) + ") SELECT " + TextUtils.join(",", arrayList) + " FROM " + concat + ";");
                    }
                    aVar.d("DROP TABLE " + concat);
                } catch (SQLException e3) {
                    e = e3;
                    clsArr = clsArr2;
                }
            }
            i2++;
            clsArr2 = clsArr;
            i = 14;
            z = true;
        }
    }
}
